/**
 * 串行实现
 */
#include <stdio.h>
#include <time.h>
#include <math.h>

#ifdef USE_DP
    typedef double real;
#else
    typedef float real;
#endif

const int N = 1000000;
const int NUM_REAPEATS = 20;

void scan(const real *x, real *y) {
    // 主程序
    for (int i=0; i<N; i++) {
        y[i] = (i == 0) ? x[i] : y[i-1] + x[i];
    }
}

void timing(const real *x, real *y) {
    float tSum = 0.0;
    float t2Sum = 0.0;
    for (int i=0; i<NUM_REAPEATS; ++i) {
        clock_t start, end; 
        start = clock();
        scan(x, y);
        end = clock();
        float elapsedTime = (float)(end - start) / CLOCKS_PER_SEC * 1000;
        tSum += elapsedTime;
        t2Sum += elapsedTime * elapsedTime;
    }
    float tAVG = tSum / NUM_REAPEATS;
    float tERR = sqrt(t2Sum / NUM_REAPEATS - tAVG * tAVG);
    printf("Time = %g +- %g ms.\n", tAVG, tERR);
}

int main() {
    real *x = new real[N];
    real *y = new real[N];
    for (int i=0; i<N; i++) x[i] = 1.23;
    timing(x, y);
    for (int i = N - 10; i < N; i++) printf("%f  ", y[i]);
    delete[] x;
    delete[] y;

    return 0;
}
